﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using _0812159_0812164.pojo;
using System.Collections.Generic;
using MySql.Data.MySqlClient;

namespace _0812159_0812164.dao
{
    public class ShareDAO
    {
        public static bool InsertShare(Share sh)
        {
            bool rs = false;
            DataHelper helper = new DataHelper();
            helper.open();
            String sql = String.Format("INSERT INTO SHARE(src, des, location) VALUES ('{0}','{1}',{2})", sh.Src,sh.Des, sh.Location);
            rs = helper.executeUpdate(sql);
            helper.close();
            return rs;
        }

        public static bool DeleteShare(int id)
        {
            return UpdateState(id, -1);
        }

        public static bool AcceptShare(int id)
        {
            return UpdateState(id, 1);
        }

        public static bool UpdateState(int id, int state)
        {
            bool rs = false;
            DataHelper helper = new DataHelper();
            helper.open();
            String sql = String.Format("UPDATE SHARE SET state = {0} WHERE id = {1}", state,id);
            rs = helper.executeUpdate(sql);
            helper.close();
            return rs;
        }

        public static List<Share> getListShare(string username)
        {
            List<Share> list = new List<Share>();
            DataHelper helper = new DataHelper();
            helper.open();
            string sql = String.Format("SELECT * FROM Share WHERE des='{0}' AND state=0",username );
            MySqlDataReader reader = helper.executeQuery(sql);
            while (reader.Read())
            {
                Share sh = new Share();
                sh.Id = (int)reader["id"];
                sh.Src=(String)reader["src"];
                sh.Des= (String)reader["des"];
                sh.Location = (int)reader["location"];
                sh.State = (int)reader["state"];
                list.Add(sh);
            }
            helper.close();

            return list;
        }

        /// <summary>
        /// update a share record
        /// </summary>
        /// <param name="share"></param>
        /// share: new infomation of a share record to updating.
        /// <returns>-1 if error occur</returns>
        public static int Update(Share share)
        {
            return -1;
        }
    }
}
